package com.hivclassifier.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import weka.classifiers.Evaluation;

import com.hivclassifier.models.HivClassModel.CoreceptorEnums;
import com.hivclassifier.utils.Logger;

public class StatisticsResultScreen extends JFrame {

	private static final long serialVersionUID = -4087930715907200615L;
	
	public StatisticsResultScreen(Evaluation evaluationResult) {
		
		setTitle("Results");
		
		Container pane = getContentPane();
		
		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		getRootPane().registerKeyboardAction(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
		
		String[] columnNamesTable1 = {CoreceptorEnums.CXCR4.name(), CoreceptorEnums.DUAL.name(), CoreceptorEnums.CCR5.name(), "Classified as"},
				 columnNamesTable2 = {"True positive", "False positive", "ROC area", "Class"};

		String[][] confusionMatrix = new String[3][4];
		double[][] matrix = evaluationResult.confusionMatrix();
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix.length; j++) {
				confusionMatrix[i][j] = String.valueOf(matrix[i][j]);
			}
		}
		
		confusionMatrix[0][3] = CoreceptorEnums.CXCR4.name();
		confusionMatrix[1][3] = CoreceptorEnums.DUAL.name();
		confusionMatrix[2][3] = CoreceptorEnums.CCR5.name();
		
		
		JTable classifyTable = new JTable(confusionMatrix, columnNamesTable1);
		classifyTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
		for (int i = 0; i < 4; i++) {
			classifyTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
		}
		JScrollPane scrollPane = new JScrollPane(classifyTable);
		pane.add(scrollPane, BorderLayout.PAGE_START);
		
		String[][] statistics = new String[3][4];
		for (int i = 0; i < matrix.length; i++) {
			statistics[i][0] = BigDecimal.valueOf(evaluationResult.truePositiveRate(i)).round(new MathContext(2, RoundingMode.HALF_EVEN)).toString();
			statistics[i][1] = BigDecimal.valueOf(evaluationResult.falsePositiveRate(i)).round(new MathContext(2, RoundingMode.HALF_EVEN)).toString();
			statistics[i][2] = BigDecimal.valueOf(evaluationResult.areaUnderROC(i)).round(new MathContext(2, RoundingMode.HALF_EVEN)).toString();
		}
		
		statistics[0][3] = "CXCR4";
		statistics[1][3] = "DUAL";
		statistics[2][3] = "CCR5";
		
		JTable statisticsTable = new JTable(statistics, columnNamesTable2);
		statisticsTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
		for (int i = 0; i < 4; i++) {
			statisticsTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
		}
		statisticsTable.setEnabled(false);
		scrollPane = new JScrollPane(statisticsTable);
		pane.add(scrollPane, BorderLayout.CENTER);
		
		if (Logger.IS_DEBUG) {
			System.out.println("-------------- BAYES ---------------- \n" + evaluationResult.toSummaryString());
			matrix = evaluationResult.confusionMatrix();
			for (int i = 0; i < matrix.length; i++) {
				System.out.println(matrix[i][0] + " | " + matrix[i][1] + " | " + matrix[i][2]);
			}
			System.out.println("TP rate for CXCR4: " + evaluationResult.truePositiveRate(0));
			System.out.println("TP rate for DUAL: " + evaluationResult.truePositiveRate(1));
			System.out.println("TP rate for CCR5: " + evaluationResult.truePositiveRate(2) + "\n");
			
			System.out.println("FP rate for CXCR4: " + evaluationResult.falsePositiveRate(0));
			System.out.println("FP rate for DUAL: " + evaluationResult.falsePositiveRate(1));
			System.out.println("FP rate for CCR5: " + evaluationResult.falsePositiveRate(2) + "\n");
			
			System.out.println(evaluationResult.areaUnderROC(2));
		}
		
		pack();
		setLocationRelativeTo(null); // center the window
		setVisible(true);
	}
}
